/**
 * Form Layout by John.
 * Functional coding by John.
 */

package Forms;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import Classes.Validate;
import Classes.teDB;
import teComponents.*;
//Code here done by Jon, Shell by Dagny unless otherwise noted
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class SuppliersForm  extends teComponents.teJinternalFrame {
	private String[] myRSNames, myRSIDs;
	private teDB dbCon;
	String[] errMsg;
	public SuppliersForm()
	{
		super(null);
		try {
			dbCon=new teDB("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@localhost:1521:orant11g", "ictoosd", "ictoosd");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		initGUI();
	}
	public SuppliersForm(teJFrame sender)
	{
		super(sender);
		try {
			dbCon=new teDB("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@localhost:1521:orant11g", "ictoosd", "ictoosd");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		initGUI();
	}
	private void initGUI() {
		try {
			{
				this.setTitle("Maintain Suppliers");
				{
					jScrollPane1 = new JScrollPane();
					getContentPane().add(jScrollPane1);
					jScrollPane1.setBounds(12, 47, 283, 231);
					{
						ListModel lstAllModel = 
							new DefaultComboBoxModel();
						lstAll = new teJList();
						jScrollPane1.setViewportView(lstAll);
						lstAll.setModel(lstAllModel);
						lstAll.setBounds(12, 47, 283, 231);
						lstAll.addListSelectionListener(new ListSelectionListener() {
							
							@Override
							public void valueChanged(ListSelectionEvent e) {
								// TODO Auto-generated method stub
								if(lstAll.getSelectedIndex()>=0)
								{
									txtSupID.setText(myRSIDs[lstAll.getSelectedIndex()]);
									txtSupName.setText(myRSNames[lstAll.getSelectedIndex()]);
									btnUpdate.setEnabled(true);
									btnDelete.setEnabled(true);
									btnAdd.setEnabled(false);
								}
							}
						});
						
					}
				}
			}
			{
				this.setSize(580, 361);
			}
			{
				lblAll = new teJLabel();
				getContentPane().add(lblAll);
				lblAll.setText("Suppliers");
				lblAll.setBounds(12, 27, 80, 14);
				lblAll.setHorizontalAlignment(SwingConstants.LEFT);
			}
			{
				lblSearch = new teJLabel();
				getContentPane().add(lblSearch);
				lblSearch.setText("Search for: ");
				lblSearch.setBounds(317, 27, 110, 14);
				lblSearch.setHorizontalAlignment(SwingConstants.LEFT);
			}
			{
				btnAdd = new teJButton();
				getContentPane().add(btnAdd);
				btnAdd.setText("Add");
				btnAdd.setBounds(14, 296, 100, 25);
				btnAdd.setToolTipText("Add a new Supplier to the database, based on the information in the 'Your Selection' form on the right.");
				btnAdd.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						// TODO Auto-generated method stub
						if(Validate.genericText(txtSupName.getText())&&!txtSupName.getText().isEmpty())
						{
							
							try {
								String[] values = {dbCon.getGenKey("Suppliers", "SupplierId"), txtSupName.getText()};
								dbCon.insert("Suppliers", values);
							} catch (SQLException e1) {
								// TODO Auto-generated catch block
								setErr();
								errMsg = new String[] {"Generic SQL error", "Check your terms or","make sure you can connect","to the database"};
								lstAll.setModel(new DefaultComboBoxModel(errMsg));
							}
							reset();
						}
						else
						{
							setErr();
							errMsg = new String[] {"Bad name!","No double dashes or semicolons"};
							lstAll.setModel(new DefaultComboBoxModel(errMsg));
						}
					}
				});
			}
			{
				btnDelete = new teJButton();
				getContentPane().add(btnDelete);
				btnDelete.setText("Delete");
				btnDelete.setBounds(159, 296, 100, 25);
				btnDelete.setToolTipText("Delete the selected Supplier from the database.");
				btnDelete.setEnabled(false);
				btnDelete.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						// TODO Auto-generated method stub
						try {
							dbCon.delete("Suppliers", "SupplierId = " + myRSIDs[lstAll.getSelectedIndex()]);
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							setErr();
							errMsg = new String[] {"Generic SQL error", "Check your terms or","make sure you can connect","to the database"};
							lstAll.setModel(new DefaultComboBoxModel(errMsg));
						}
						reset();
					}
				});
			}
			{
				btnClear = new teJButton();
				getContentPane().add(btnClear);
				btnClear.setText("Clear");
				btnClear.setBounds(451, 296, 100, 25);
				btnClear.setToolTipText("Clears all the information in the forms and refreshes the Supplier list with all Suppliers in the database.");
				btnClear.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						// TODO Auto-generated method stub
						reset();
						
					}
				});
			}
			{
				btnUpdate = new teJButton();
				getContentPane().add(btnUpdate);
				btnUpdate.setText("Update");
				btnUpdate.setBounds(305, 296, 100, 25);
				btnUpdate.setToolTipText("Update the selected Supplier in the database.");
				btnUpdate.setEnabled(false);
				btnUpdate.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						// TODO Auto-generated method stub
						if(Validate.genericText(txtSupID.getText())&&!txtSupID.getText().isEmpty())//set validator
						{
							String[] feild ={"SupName"}, value ={txtSupName.getText()};
							try {
								dbCon.update("Suppliers", feild, value, "SupplierId = "+txtSupID.getText());
							} catch (SQLException e1) {
								// TODO Auto-generated catch block
								setErr();
								errMsg = new String[] {"Generic SQL error", "Check your terms or","make sure you can connect","to the database"};
								lstAll.setModel(new DefaultComboBoxModel(errMsg));
							}
							reset();
						}
						else
						{
							setErr();
							errMsg = new String[] {"Bad name!","No double dashes or semicolons"};
							lstAll.setModel(new DefaultComboBoxModel(errMsg));
						}
					}
				});
				
			}
			{
				pnlSelected = new teJPanel();
				getContentPane().add(pnlSelected);
				pnlSelected.setBounds(317, 158, 234, 118);
				pnlSelected.setLayout(null);
				{
					lblSupName = new teJLabel();
					pnlSelected.add(lblSupName);
					lblSupName.setText("Supplier Name: ");
					lblSupName.setBounds(8, 66, 105, 14);
				}
				{
					txtSupName = new teJTextField();
					pnlSelected.add(txtSupName);
					txtSupName.setBounds(8, 86, 215, 18);
				}
				{
					txtSupID = new teJTextField();
					pnlSelected.add(txtSupID);
					txtSupID.setBounds(8, 33, 213, 18);
					txtSupID.setEditable(false);
				}
				{
					lblSupID = new teJLabel();
					pnlSelected.add(lblSupID);
					lblSupID.setText("Supplier ID: ");
					lblSupID.setBounds(8, 13, 82, 14);
				}
			}
			{
				lblSelectedPanel = new teJLabel();
				getContentPane().add(lblSelectedPanel);
				lblSelectedPanel.setText("Your Selection:");
				lblSelectedPanel.setBounds(317, 138, 101, 14);
				lblSelectedPanel.setHorizontalAlignment(SwingConstants.LEFT);
			}
			{
				pnlSearch = new teJPanel();
				getContentPane().add(pnlSearch);
				pnlSearch.setBounds(317, 47, 234, 79);
				pnlSearch.setLayout(null);
				{
					txtSearch = new teJTextField();
					pnlSearch.add(txtSearch);
					txtSearch.setBounds(13, 14, 208, 21);
				}
				{
					btnGo = new teJButton();
					pnlSearch.add(btnGo);
					btnGo.setText("Search");
					btnGo.setBounds(13, 45, 208, 25);
					btnGo.setToolTipText("Searches the Suppliers table for Suppliers that match the parameters in the search panel.");
					btnGo.addActionListener(new ActionListener() {
						
						@Override
						public void actionPerformed(ActionEvent e) {
							// TODO Auto-generated method stub
							if(Validate.genericText(txtSearch.getText()))
							{
								lstPopulate(txtSearch.getText().toUpperCase());
							}
							else
							{
								setErr();
								errMsg = new String[] {"Malformed search term!","No double dashes or semicolons"};
								lstAll.setModel(new DefaultComboBoxModel(errMsg));
							}
						}
					});
					lstPopulate("");
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	private void lstPopulate(String search)
	{
		try {
			ResultSet rs = dbCon.getCustomRS("SELECT SupplierId, SupName FROM Suppliers WHERE upper(SupName) like '%"+search+"%' ORDER BY SupName"); //ResultSet("Suppliers", "SupplierId , SupName", "SupName = '" + txtSearch.getText()+"'");
			ArrayList<String> name = new ArrayList<String>(), ID = new ArrayList<String>();
			while(rs.next())
			{
				ID.add(rs.getString(1));
				name.add(rs.getString(2));
			}
			myRSIDs = new String[ID.size()];
			myRSNames = new String[name.size()];
			ID.toArray(myRSIDs);
			name.toArray(myRSNames);
			lstAll.setModel(new DefaultComboBoxModel(myRSNames));
			//dbCon.close();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			setErr();
			errMsg = new String[] {"Generic SQL error", "Check your terms or","make sure you can connect","to the database"};
			lstAll.setModel(new DefaultComboBoxModel(errMsg));
		}
	}
	private void reset()
	{
		btnDelete.setEnabled(false);
		btnUpdate.setEnabled(false);
		btnAdd.setEnabled(true);
		btnGo.setEnabled(true);
		lstAll.setModel(new DefaultComboBoxModel());
		txtSupID.setText("");
		txtSupName.setText("");
		txtSearch.setText("");
		lstPopulate("");
		//The following is by Dagny
		lstAll.setForeground(new java.awt.Color(43,31,2));
		
		//reset the colour of the selection background
		lstAll.setSelectionBackground(new java.awt.Color(2,234,231));
		//set focusable back to true
		lstAll.setFocusable(true);
	}
	private void setErr()
	{//Dagny's Code, implemented by Jon
		lstAll.setForeground(new java.awt.Color(198,42,3));

		//set the selection colour to the background so 
		//it doesn't look like anything's getting selected
		lstAll.setSelectionBackground(lstAll.getBackground());
		//set focusable to false
		lstAll.setFocusable(false);
		//Following by Jon
		btnAdd.setEnabled(false);
		btnGo.setEnabled(false);
		btnDelete.setEnabled(false);
		btnUpdate.setEnabled(false);
	}
	private teJList lstAll;
	private teJButton btnGo;
	private JScrollPane jScrollPane1;
	private teJPanel pnlSearch;
	private teJLabel lblSelectedPanel;
	private teJLabel lblSupID;
	private teJTextField txtSupID;
	private teJLabel lblSupName;
	private teJTextField txtSupName;
	private JPanel pnlSelected;
	private teJButton btnAdd;
	private teJButton btnUpdate;
	private teJButton btnClear;
	private teJButton btnDelete;
	private teJLabel lblSearch;
	private teJTextField txtSearch;
	private teJLabel lblAll;

}
